import * as PDFJS from 'pdfjs-dist';
import pdfjsWorker from 'pdfjs-dist/build/pdf.worker.entry';

PDFJS.GlobalWorkerOptions.workerSrc = pdfjsWorker;

const loadingParams = {
  cMapUrl: 'https://unpkg.com/pdfjs-dist@2.0.943/cmaps/',
  cMapPacked: true,
};

export const loadingPdf = (url) => {
  const loadingTask = PDFJS.getDocument({
    ...loadingParams,
    url,
  });
  return loadingTask;
};

export async function getPageSize({ url, page }) {
  const loadingTask = PDFJS.getDocument({
    ...loadingParams,
    url,
  });
  const pdf = await loadingTask.promise;
  const pageObj = await pdf.getPage(page);
  const viewPort = pageObj.getViewport({ scale: 1 });
  return {
    width: viewPort.width,
    height: viewPort.height,
  }
}
